package twentytwentyone_3;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Zahi
 * @Description:
 * @Date Created in 2021-03-25 13:38
 * @Modified By:
 */
public class SelfDividingNumbers_728 {
    public static void main(String[] args){
        int left = 1 ,right = 22;
        System.out.println(selfDividingNumbers(left, right));
    }

    public static List<Integer> selfDividingNumbers(int left,int right){
        List<Integer> result = new ArrayList<>();
        while (left <= right){
            if (isSelfDividingNumbers(left)) result.add(left);
            left++;
        }
        return result;
    }

    public static boolean isSelfDividingNumbers(int number){
        String s = String.valueOf(number);
        if (s.contains("0")) return false;
        List<Integer> save = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            save.add(Integer.parseInt(s.substring(i,i + 1)));
        }
        boolean flag = true;
        for (int i = 0; i < s.length(); i++) {
            if (number % save.get(i) != 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }
}
